package util;

import java.util.List;

import org.joda.time.DateTime;

import systemconfig.SystemConfiguration;

public class Holiday {

    // ---------INSTANCE VARIABLES-----------------------------------------//

    private DateTime date;

    private String description;

    // ---------CONSTRUCTORS------------------------------------------------//

    public Holiday(final DateTime aDate, final String aDescription) {
        this.date = aDate;
        this.description = aDescription;
    }

    // ---------ACCESSORS---------------------------------------------------//

    public DateTime getDate() {
        return this.date;
    }

    public void setDate(final DateTime date) {
        this.date = date;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    // ---------OTHER BEHAVIOUR---------------------------------------------//

    public DateTime getNextNonHolidayDateFrom(final DateTime aDateTime, final Holiday aHoliday) {
        boolean found = false;
        DateTime ret = aDateTime;

        while (!found) {
            if (!SystemConfiguration.getInstance().getHolidayList().contains(ret) && ret.getDayOfWeek() < 6) {
                found = true;
            } else {
                ret = ret.plusDays(1);
            }
        }
        return ret;
    }

    public DateTime getNextNonHolidayDate(final Holiday aHoliday) {
        return this.getNextNonHolidayDateFrom(DateTime.now(), aHoliday);
    }

    public DateTime getNonHolidayDateFrom(final DateTime aDate, final int plusDays, final List<Holiday> holidays) {
        DateTime newDate = aDate.plusDays(plusDays);
        while (this.isHoliday(newDate, holidays)) {
            newDate = newDate.plusDays(1);
        }
        return newDate;
    }

    public boolean isHoliday(final DateTime aDate, final List<Holiday> holidays) {
        for (Holiday hd : holidays) {
            if (hd.getDate().getDayOfYear() == aDate.getDayOfYear()) {
                return true;
            }
        }
        return false;
    }
}
